package org.gdstash.ui.select;

import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;

public class ItemSetSelectionPane extends AdjustablePanel {
    private ItemRarityPane pnlItemRarity;
    private LevelSelPane pnlLevel;

    public ItemSetSelectionPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlItemRarity)).addGroup(layout.createParallelGroup().addComponent(this.pnlLevel));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlItemRarity).addComponent(this.pnlLevel));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlItemRarity, this.pnlLevel);
        layout.linkSize(1, this.pnlItemRarity, this.pnlLevel);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SET_INFO"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.pnlItemRarity == null) {
            this.pnlItemRarity = new ItemRarityPane(0);
        } else {
            this.pnlItemRarity.adjustUI();
        }
        if (this.pnlLevel == null) {
            this.pnlLevel = new LevelSelPane(0);
        } else {
            this.pnlLevel.adjustUI();
        }
    }

    public void clear() {
        this.pnlItemRarity.clear();
        this.pnlLevel.clear();
    }

    public int getMinLevel() {
        if (this.pnlLevel == null) {
            return -1;
        }
        return this.pnlLevel.getMinLevel();
    }

    public int getMaxLevel() {
        if (this.pnlLevel == null) {
            return -1;
        }
        return this.pnlLevel.getMaxLevel();
    }

    public List<String> getRarityList() {
        if (this.pnlItemRarity == null) {
            return new LinkedList<String>();
        }
        return this.pnlItemRarity.getRarityList();
    }
}
